package com.liuh.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.liuh.dao.CategoryInfoDao;
import com.liuh.dao.impl.CategoryInfoDaoImpl;
import com.liuh.pojo.CategoryInfo;
import com.liuh.pojo.OrderInfo;
import com.liuh.pojo.OrderInfoVO;
import com.liuh.pojo.ProductInfo;
import com.liuh.service.CategoryService;
import com.liuh.util.Page;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;

public class CategoryServiceImpl implements CategoryService{

	CategoryInfoDao infoDao=new CategoryInfoDaoImpl();
	
	@Override
	public List<CategoryInfo> categoryByParentId(int id) {
		// TODO Auto-generated method stub
		return infoDao.cateInfoByParentId(id);
	}

	@Override
	public Map<Integer, OrderInfoVO> orders() {
		// TODO Auto-generated method stub
		List<OrderInfo> orders=infoDao.orders();
		Map<Integer, OrderInfoVO> map=CollUtil.newHashMap();
		for (OrderInfo orderInfo : orders) {
			if(map.get(orderInfo.getId())==null) {
				map.put(orderInfo.getId(), wrapOrderInfoVO(orderInfo));
			}else {
				map.get(orderInfo.getId()).getDetails().add(wrapProductInfo(orderInfo));
			}
		}
		return map;
	}
	
	private OrderInfoVO wrapOrderInfoVO(OrderInfo orderInfo) {
		OrderInfoVO infoVO=new OrderInfoVO();
		infoVO.setCost(orderInfo.getCost());
		infoVO.setId(orderInfo.getId());
		infoVO.setOrderAdd(orderInfo.getUserAddress());
		infoVO.setLoginName(orderInfo.getLoginName());
		List<Dict> details=CollUtil.newArrayList();
		details.add(wrapProductInfo(orderInfo));
		infoVO.setDetails(details);
		return infoVO;
	}
	
	/**
	 * @note 产品信息
	 * @author liuh
	 * @date 2021年5月31日 下午6:49:46
	 */
	private Dict wrapProductInfo(OrderInfo orderInfo) {
		return Dict.create().set("proName", orderInfo.getName()).set("proNum", orderInfo.getProductNum())
		.set("proTotalCost", orderInfo.getProductTotalCost()).set("proImgUrl", orderInfo.getFileName());
	}

	@Override
	public List<CategoryInfo> cateInfoByProId(int id) {
		// TODO Auto-generated method stub
		return infoDao.cateInfoByProId(id);
	}

	@Override
	public Dict cateInfoByProId2(int id) {
		// TODO Auto-generated method stub
		return infoDao.cateInfoByProId2(id);
	}

	@Override
	public Page<CategoryInfo> catePage(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		Page<CategoryInfo> page=new Page<>();
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);
		page.setTotalRow(infoDao.cateInfoCount());
		page.setData(infoDao.cateInfoPage(pageIndex, pageSize));
		return page;
	}

	@Override
	public List<CategoryInfo> findCate12() {
		// TODO Auto-generated method stub
		return infoDao.cateInfoBy12();
	}

	@Override
	public CategoryInfo findCateAndParent(int cateId) {
		// TODO Auto-generated method stub
		List<CategoryInfo> cate=infoDao.findCateInfoAndParentId(cateId);
		return cate.size()>0?cate.get(0):new CategoryInfo();
	}

	@Override
	public List<ProductInfo> findProductByIds(Set<String> ids) {
		// TODO Auto-generated method stub
		return infoDao.findProductByIds(ids);
	}

}
